package com.fuze.energy.service.impl;


import cn.hutool.core.date.DateUtil;
import com.fuze.energy.common.Advice;
import com.fuze.energy.common.CustomException;
import com.fuze.energy.domain.dto.OzoneDTO;
import com.fuze.energy.domain.entity.MonitoringData;
import com.fuze.energy.mapper.OzoneMapper;
import com.fuze.energy.mapper.WarningMapper;
import com.fuze.energy.service.OzoneService;
import com.fuze.energy.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OzoneServiceImpl implements OzoneService {

    @Autowired
    OzoneMapper ozoneMapper;

    @Autowired
    WarningMapper warningMapper;

    @Override
    public Integer currentOzo(String mainBoardId) {
        MonitoringData monitoringData = ozoneMapper.selectOzo(mainBoardId);
        if (monitoringData == null) {
            throw new CustomException(Advice.FAILED_QUERY);
        }
        /**
         * 监测到臭氧存在即发出告警，记录告警信息
         * 采集器类型（温度：1；湿度：2；臭氧：3）
         */
        Integer smokeDenstiy = monitoringData.getSmokeDenstiy();
        if (smokeDenstiy == 1) {
            boolean HumWarningType = warningMapper.recordWarning(monitoringData.getRmuName(), (double) smokeDenstiy, 2, new Date());
            if (HumWarningType) {
                System.out.println("=======臭氧告警数据记录成功=======");
            } else {
                throw new CustomException(Advice.RECORD_OZO_FALL);
            }
        }
        return monitoringData.getSmokeDenstiy();
    }

    @Override
    public List<OzoneDTO> todayOzo(String mainBoardId) {
        List<MonitoringData> ozoDataList = ozoneMapper.selectToday(TimeUtils.getBegin().toString(),
                TimeUtils.getEng().toString(),
                mainBoardId);
        ArrayList<OzoneDTO> list = new ArrayList<>();
        ozoDataList.forEach(ozoData -> {
            list.add(new OzoneDTO(ozoData.getRmuName(),
                    DateUtil.formatDateTime(ozoData.getDate()),
                    ozoData.getSmokeDenstiy()));
        });
        return list;
    }
}
